//
//  ViewController.m
//  Timer
//
//  Created by LeeJay on 16/4/19.
//  Copyright © 2016年 LeeJay. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
//    [self timer];
    [self timer2];
}

/**
 *  自动加入RunLoop
 */
- (void)timer {
    
    // scheduledTimerWithTimeInterval创建的定时器自动加入CurrentRunLoop，并且运行在NSDefaultRunLoopMode下
    [NSTimer scheduledTimerWithTimeInterval:2.0
                                     target:self
                                   selector:@selector(run)
                                   userInfo:nil
                                    repeats:YES];
}

/**
 *  手动加入RunLoop
 */
- (void)timer2 {
    
    // timerWithTimeInterval创建的定时器需要手动加入CurrentRunLoop，运行在指定Modes下，否则不会启动
    NSTimer *timer = [NSTimer timerWithTimeInterval:2.0
                                     target:self
                                   selector:@selector(run)
                                   userInfo:nil
                                    repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    
    /*
     因为这是在主线程，所以currentRunLoop就是mainRunLoop。RunLoop已经默认启动。
     如果在子线程，RunLoop需要手动启动。
     [[NSRunLoop currentRunLoop] run];
    */
}

- (void)run {
    
    NSLog(@"----run----");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
